package com.aiti.lulian.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.bo.CardAccessRecordBo;
import com.aiti.lulian.entity.CardAccessRecord;
import com.aiti.lulian.entity.LuLianEnterpriseUser;
import com.aiti.lulian.mapper.CardAccessRecordMapper;
import com.aiti.lulian.mapper.LuLianEnterpriseUserMapper;
import com.aiti.lulian.service.ICardAccessRecordService;
import com.aiti.lulian.vo.CardAccessRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CardAccessRecordServiceImpl extends BaseSerivceImpl<CardAccessRecordMapper, CardAccessRecord> implements ICardAccessRecordService {

    private final CardAccessRecordMapper cardAccessRecordMapper;

    private final LuLianEnterpriseUserMapper enterpriseUserMapper;

    public CardAccessRecordServiceImpl(CardAccessRecordMapper cardAccessRecordMapper,LuLianEnterpriseUserMapper enterpriseUserMapper) {
        this.cardAccessRecordMapper = cardAccessRecordMapper;
        this.enterpriseUserMapper = enterpriseUserMapper;
    }

    /**
     * 通过企业Id获取访问数量
     *
     * @return
     */
    @Override
    public Message getCardAccessCountByEnterpriseId(String userId, CardAccessRecordBo bo) {
        String roleIds = bo.getRoleIds();
        String[] roleArgs = roleIds.split(",");
        List<CardAccessRecord> cardAccessRecord = null;
        CardAccessRecordVo cardAccessRecordVo = CardAccessRecordVo.builder().build();
        //企业用户分支
        if(ArrayUtil.containsAny(roleArgs,"8836c75af939406a9f41128826f6d8b3")){

            LuLianEnterpriseUser luLianEnterpriseUser = enterpriseUserMapper.selectOne(new QueryWrapper<LuLianEnterpriseUser>().eq("user_id", userId));
            if(luLianEnterpriseUser == null){
                cardAccessRecordVo.setTotalCount(0L);
                cardAccessRecordVo.setToDayCount(0L);
                cardAccessRecordVo.setDailyQuantityDiff("0");
                return Message.success(cardAccessRecordVo);
            }
            cardAccessRecord = cardAccessRecordMapper.selectList(new QueryWrapper<CardAccessRecord>().eq("is_del", false).or().eq("is_del", true).and(i -> i.eq("enterprise_id", luLianEnterpriseUser.getEnterpriseId())));
        }else if (ArrayUtil.containsAny(roleArgs,"24d3f470d5784d2ba5f11f70f0b66af5")) {
            //企业员工用户分支
            cardAccessRecord = cardAccessRecordMapper.selectList(new QueryWrapper<CardAccessRecord>().eq("user_id", userId));
        }else{
            return Message.fail("当前用户不是企业或者企业员工权限");
        }
        if(cardAccessRecord == null || cardAccessRecord.isEmpty()){
            cardAccessRecordVo.setTotalCount(0L);
            cardAccessRecordVo.setToDayCount(0L);
            cardAccessRecordVo.setDailyQuantityDiff("0");
        }else{
            //构建总数
            long totalCount = cardAccessRecord.stream().mapToLong(k -> k.getTotalCount() == null ? 0L : k.getTotalCount()).sum();
            long yesterdayCount = cardAccessRecord.stream().mapToLong(k -> k.getYesterdayCount() == null ? 0L : k.getYesterdayCount()).sum();
            long toDayCount = cardAccessRecord.stream().mapToLong(k -> k.getToDayCount() == null ? 0L : k.getToDayCount()).sum();
            cardAccessRecordVo.setTotalCount(totalCount);
            cardAccessRecordVo.setToDayCount(toDayCount);
            Long diff = toDayCount - yesterdayCount;
            switch (diff.compareTo(0L)){
                case 1:
                    cardAccessRecordVo.setDailyQuantityDiff("+" + diff);
                    break;
                case -1:
                    cardAccessRecordVo.setDailyQuantityDiff(diff + "");
                    break;
                case 0:
                default:
                    cardAccessRecordVo.setDailyQuantityDiff("0");
                    break;
            }
        }
        return Message.success(cardAccessRecordVo);
    }
}
